package com.nanfang51g3.ckz.ui.forget;

import android.text.TextUtils;

import com.nanfang51g3.ckz.di.ApiModule;
import com.nanfang51g3.ckz.domain.HttpResultBean;
import com.nanfang51g3.ckz.domain.SuccessBean;
import com.nanfang51g3.ckz.exec.Api2Exception;
import com.nanfang51g3.ckz.http.HttpResultFunc;
import com.nanfang51g3.ckz.http.RestApi;
import com.nanfang51g3.ckz.http.SubscriberInterface;
import com.nanfang51g3.ckz.utils.KLog;
import com.nanfang51g3.ckz.utils.MD5Utils;
import com.nanfang51g3.ckz.utils.ToolUtils;

import java.util.TreeMap;

import javax.inject.Inject;

import retrofit2.Retrofit;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/4/13.
 */
public class ForgetPasswordPresenter implements ForgetPasswordContract.ForgetPasswordActionsListener {

    String message;
    private final RestApi _restApi;
    private final ForgetPasswordContract.View mView;

    @Inject
    public ForgetPasswordPresenter(RestApi restApi, ForgetPasswordContract.View mView) {
        KLog.i("TAG", "进入了...PreviewMoneyInfoPresenter");
        /**
         * @see ApiModule#provideRestApi(Retrofit)
         */
        this._restApi = restApi;
        KLog.i("TAG", _restApi == null ? "进入了...is null" : "进入了...not null");
        /**
         * @see ForgetPasswordPresenterModule#ForgetPasswordPresenterModule(ForgetPasswordContract.View)
         */
        this.mView = mView;
        KLog.i("TAG", mView == null ? "进入了...is null" : "进入了...not null");
    }

    @Override
    public void getForgetPasswordCode(String phone,final boolean isStart) {
        if (TextUtils.isEmpty(phone)) {
            mView.showMessage("请输入手机号");
            return;
        }
        if (!ToolUtils.isMobileNO(phone)) {
            mView.showMessage("请输入正确手机号");
            return;
        }
        TreeMap<String, String> treeMap = new TreeMap<>();
        treeMap.put("phone", phone);
        String sign = MD5Utils.getAccessToken(treeMap);
        Observable observable = _restApi.forgetpassSendcode(phone, sign);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new CodeResultFunc<SuccessBean>())
                .subscribe(new SubscriberInterface<SuccessBean>() {
                    @Override
                    public void onCompleted() {
                        KLog.i("Evd", "onCompleted");
                    }

                    @Override
                    public void _onError(String msg) {
                        KLog.i("Evd", "onError " + msg);
                        /**
                         * @see  ForgetPassword1Activity#showMessage(String)
                         */
                        mView.showMessage(msg);
                    }

                    @Override
                    public void onNext(SuccessBean o) {
                        KLog.i("Evd", "onNext " + o.toString());
                        /**
                         * @see  ForgetPassword1Activity#showMessage(String)
                         */
                        mView.showMessage(message);
                        if(isStart){
                            /**
                             * @see  ForgetPassword1Activity#startShowView()
                             */
                            mView.startShowView();
                        }

                    }
                });


    }


    @Override
    public void verificationCode(String code, String phone) {
        if (TextUtils.isEmpty(code)) {
            mView.showMessage("请输入验证码");
            return;
        }
        TreeMap<String, String> treeMap = new TreeMap<>();
        treeMap.put("code", code);
        treeMap.put("phone", phone);
        String sign = MD5Utils.getAccessToken(treeMap);
        KLog.i("Evd", treeMap.toString() + "-" + sign);
        Observable observable = _restApi.forgetpassVerifycode(code, phone, sign);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new CodeResultFunc<SuccessBean>())
                .subscribe(new SubscriberInterface<SuccessBean>() {
                    @Override
                    public void onCompleted() {
                        KLog.i("Evd", "onCompleted");
                    }

                    @Override
                    public void _onError(String msg) {
                        KLog.i("Evd", "onError " + msg);
                        /**
                         * @see  ForgetPassword2Activity#showMessage(String)
                         */
                        mView.showMessage(msg);

                    }

                    @Override
                    public void onNext(SuccessBean o) {
                        KLog.i("Evd", "SuccessBean " + o.toString());
                        /**
                         * @see  ForgetPassword2Activity#showMessage(String)
                         */
                        mView.showMessage(message);
                        /**
                         * @see ForgetPassword2Activity#startShowView()
                         */
                        mView.startShowView();
                    }
                });


    }

    @Override
    public void forget(String phone, String password, String confirmPassword) {
        if (TextUtils.isEmpty(phone)) {
            mView.showMessage("请输入手机号");
            return;
        }
        TreeMap<String, String> treeMap = new TreeMap<>();
        treeMap.put("phone", phone);
        treeMap.put("password", password);
        String sign = MD5Utils.getAccessToken(treeMap);
        Observable observable = _restApi.forgetpass(phone, password, sign);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new CodeResultFunc<SuccessBean>())
                .subscribe(new SubscriberInterface<SuccessBean>() {
                    @Override
                    public void onCompleted() {
                        KLog.i("Evd", "onCompleted");
                    }

                    @Override
                    public void _onError(String msg) {
                        KLog.i("Evd", "onError " + msg);
                        /**
                         * @see  ForgetPassword3Activity#showMessage(String)
                         */
                        mView.showMessage(msg);

                    }

                    @Override
                    public void onNext(SuccessBean o) {
                        KLog.i("Evd", "onNext " + o.toString());
                        /**
                         * @see  ForgetPassword3Activity#showMessage(String)
                         */
                        mView.showMessage(message);
                        /**
                         * @see  ForgetPassword3Activity#startShowView()
                         */
                        mView.startShowView();
                    }
                });
    }

    class CodeResultFunc<T> extends HttpResultFunc<T> {
        @Override
        public T call(HttpResultBean<T> httpResult) {
            KLog.i("Evd", httpResult == null ? "1-" : "0-" + httpResult.getCode() + "-" + httpResult.getInfo() + "-" + httpResult.getData().toString());
            if (
                    httpResult.getCode() == 2 ||
                            httpResult.getCode() == 3 ||
                            httpResult.getCode() == 4 ||
                            httpResult.getCode() == 5 ||
//                            httpResult.getCode() == 6 ||
                            httpResult.getCode() == 7 ||
                            httpResult.getCode() == 8 ||
//                            httpResult.getCode() == 9 ||
                            httpResult.getCode() == 10 ||
                            httpResult.getCode() == 11 ||
                            httpResult.getCode() == 12) {
                throw new Api2Exception(httpResult.getInfo());
            }
            message = httpResult.getInfo();
            return httpResult.getData();
        }
    }

}
